package com.emep.changzhi.analyse.service.statistics;

import com.emep.changzhi.analyse.entity.aqi.DataAqiInfo;
import com.emep.changzhi.analyse.repository.aqi.DataAqiInfoRepository;
import com.emep.changzhi.analyse.service.outlet.SysOutletInfoService;
import com.emep.changzhi.analyse.utils.DateTimeUtil;
import com.emep.changzhi.analyse.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import sun.nio.cs.ext.MacHebrew;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class GetDayAqiService {


    @Autowired
    private DataAqiInfoRepository dataAqiInfoRepository;

    private static final Logger logger = LoggerFactory.getLogger(SysOutletInfoService.class);

    /**
     * 查询输入时间段内的数据
     *
     * @return
     */
    public List<DataAqiInfo> findapiByTime(Map<String, Object> payload) {
        List<DataAqiInfo> dataAqiInfoList = new ArrayList<>();

        //行政区划
        String 	cityCode = payload.get("cityCode") != null ? payload.get("cityCode").toString() : "";
        //站点名称
        String siteName = payload.get("siteName") != null ? payload.get("siteName").toString() : "";
        //开始时间
        String startTime = payload.get("startTime")!= null ? payload.get("startTime").toString():"";
        //结束时间
        String endTime = payload.get("endTime")!= null ? payload.get("endTime").toString():"";
        try {
        if (!StringUtils.isEmpty(cityCode) && !StringUtils.isEmpty(siteName) && !StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            //开始时间
            String start=String.valueOf( DateTimeUtil.stringToDate(startTime,DateTimeUtil.DATETIME_DEFAULT_FORMAT).getTime());
            //结束时间
            String end= String.valueOf( DateTimeUtil.stringToDate(endTime,DateTimeUtil.DATETIME_DEFAULT_FORMAT).getTime());
            dataAqiInfoList = dataAqiInfoRepository.findAll(new Specification<DataAqiInfo>() {
                @Override
                public javax.persistence.criteria.Predicate toPredicate(Root<DataAqiInfo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                    javax.persistence.criteria.Predicate p1 = null;
                    javax.persistence.criteria.Predicate p2 = null;
                    javax.persistence.criteria.Predicate p3 = null;
                    javax.persistence.criteria.Predicate p4 = null;
                        //大于或等于传入时间
                        p1 = criteriaBuilder.greaterThanOrEqualTo(root.get("timePoint").as(String.class), start);
                        //小于于或等于传入时间
                        p2 = criteriaBuilder.lessThanOrEqualTo(root.get("timePoint").as(String.class), end);
                        //站点名称
                        p3 = criteriaBuilder.equal(root.get("positionName").as(String.class), siteName);
                        //行政区划
                        p4 = criteriaBuilder.equal(root.get("areaId").as(String.class), cityCode);

                    query.where(criteriaBuilder.and(p1, p2, p3,p4));
                    return query.getRestriction();
                }
            });



        }
        } catch (Exception e)

        {
            logger.error(e.getLocalizedMessage());
        }

        return dataAqiInfoList;
    }




    public List<String> getaqinum(String siteName,String pollution,String startime,String endtime){
        List<String> result=new ArrayList<>();

      List<DataAqiInfo>  dataAqiInfoList = dataAqiInfoRepository.findAll(new Specification<DataAqiInfo>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<DataAqiInfo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                javax.persistence.criteria.Predicate p1 = null;
                javax.persistence.criteria.Predicate p2 = null;
                javax.persistence.criteria.Predicate p3 = null;
                //大于或等于传入时间
                p1 = criteriaBuilder.greaterThanOrEqualTo(root.get("timePoint").as(String.class), startime);
                //小于于或等于传入时间
                p2 = criteriaBuilder.lessThanOrEqualTo(root.get("timePoint").as(String.class), endtime);
                //站点名称
                p3 = criteriaBuilder.equal(root.get("positionName").as(String.class), siteName);


                query.where(criteriaBuilder.and(p1, p2, p3));
                return query.getRestriction();
            }
        });

      switch (pollution){
          case "co":
              for ( DataAqiInfo d:dataAqiInfoList){
                  result.add(d.getCo());
              }
              break;

          case "pm25":
              for ( DataAqiInfo d:dataAqiInfoList){
                  result.add(d.getPm25());
              }
              break;
          case "no2":
              for ( DataAqiInfo d:dataAqiInfoList){
                  result.add(d.getNo2());
              }
              break;
          case "pm10":
              for ( DataAqiInfo d:dataAqiInfoList){
                  result.add(d.getPm25());
              }
              break;

          case "o3":
              for ( DataAqiInfo d:dataAqiInfoList){
                  result.add(d.getO3());
              }
              break;


          case "so2":
              for ( DataAqiInfo d:dataAqiInfoList){
                  result.add(d.getSo2());
              }
              break;

      }




        return result;
    }
}


